package cn.ssj.controller;

import cn.ssj.service.BaseService;
import cn.ssj.utils.JSONUtil;
import cn.ssj.utils.UploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.Page;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.List;

/**
 * Created by Administrator on 2017/9/4 0004.
 */
@Configuration
public abstract class BaseController<T,ID extends Serializable> {

    @Autowired
    private BaseService<T,ID> service;

    //文件路径
    protected static String filePath;

    @Value("${web.upload-location}")
    protected String locationPath;
    @Value("${web.upload-handler}")
    protected String handlerPath;

    protected static final String USE_FLAG_NORMAL="正在使用";

    protected static final String USE_FLAG_STOP="已停用";

    /**
     * 添加
     * @param entity 实体
     * @return
     */
    public String add(T entity){
        if(null==entity){
            return "error";
        }
        T newEntity=service.insert(entity);
        return JSONUtil.objectToJSON(entity,null);
    }

    /**
     * 修改
     * @param entity 实体
     * @return
     */
    public String update(T  entity){
        if(null==entity){
            return "error";
        }
        T newEntity=service.update(entity);
        return JSONUtil.objectToJSON(entity,null);
    }

    /**
     * 批量删除
     * @param ids id数组
     * @return
     */
    public String delete(ID[] ids){
        if(null==ids){
            return "error";
        }
        for (ID id:ids) {
            boolean flag= service.delete(id);
            if (!flag){
                return "error";
            }
        }
        return "ok";
    }

    /**
     * 分页查询
     * @param pageNumber 当前页数
     * @return
     */
    public String queryAllByPage(Integer pageNumber){
        if (null==pageNumber){
            //默认查询第一页
            pageNumber=1;
        }
        Page<T> page=service.queryByPage(pageNumber,5);
        List<T> list=page.getContent();
        if (list.isEmpty()){
            return "error";
        }
        return JSONUtil.objctToJSONArray(list,null);
    }

    /**
     * 将实体显示在前台页面中
     * @param entity 实体
     * @return 实体json字符串
     */
    public String updateToNormal(T entity){
        return this.updatByUse(entity,USE_FLAG_NORMAL);
    }

    public String updateToStop(T entity){
        return this.updatByUse(entity,USE_FLAG_STOP);
    }
    /**
     * 设置实体的使用标记
     * @param entity 实体
     * @param useFlag 使用标记
     * @return 实体json字符串
     */
    protected String updatByUse(T entity,String useFlag){
        try {
            BeanInfo bean= Introspector.getBeanInfo(entity.getClass());
            PropertyDescriptor[] propertyDescriptors=bean.getPropertyDescriptors();
            for (PropertyDescriptor propertyDescriptor:propertyDescriptors) {
                if(propertyDescriptor.getName().equals("useFlag")){
                    Method write=propertyDescriptor.getWriteMethod();
                    write.invoke(entity,useFlag);
                    break;
                }
            }
            T newEntity=service.update(entity);
            return JSONUtil.objectToJSON(newEntity,null);
        }catch (Exception e){
            return "error";
        }
    }
    public String getLocationPath() {
        return locationPath;
    }

    public void setLocationPath(String locationPath) {
        this.locationPath = locationPath;
    }

    public String getHandlerPath() {
        return handlerPath;
    }

    public void setHandlerPath(String handlerPath) {
        this.handlerPath = handlerPath;
    }

    /**
     * 图片上传
     * @param file 文件
     * @param request
     * @return
     */
    public String upload(MultipartFile file, HttpServletRequest request){
        String path= UploadUtils.upload(file,locationPath,handlerPath,request);
        if (!"error".equals(path)){
            this.filePath=path;
        }
        return path;

    }

}
